import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router/index'
import store from './store'
import axios from "axios";


import "./assets/font_4243218_aybomilsgf7/iconfont.css"

//导入elementPlus icon 
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//导入elementplus
import ElementPlus from 'element-plus'
//导入elementplus样式
import 'element-plus/dist/index.css'



// 插入进度条组件:导入ngrogress
import NProgress from 'nprogress'
//导入ngrogress样式
import "nprogress/nprogress.css"

axios.defaults.withCredentials = true;//允许跨域带cookie


// 定义路由前置守卫
router.beforeEach((to, from, next) => {
    // 路由发生变化修改页面title 
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if(localStorage.getItem("userInfo")){ //在本地中获取session，session有值放行
        next()
    }else{
        if(to.path=="/login"||to.path=="/persionRegister"||to.path=="/enterpriseRegister"||to.path=="/resetPw"){
            next()
        }else{
            // next({path:"/login"})
        }}
    NProgress.start();
    next()
})
// 定义路由后置守卫
router.afterEach((to, from) => {
    NProgress.done();
})

const app = createApp(App)

// 注册 ElementPlus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 全局错误处理，隐藏 Uncaught runtime errors
app.config.errorHandler = (err, instance, info) => {
  // 静默处理错误，不显示默认的错误覆盖层
  console.error('Vue Error:', err, info)
  // 可以在这里添加自定义的错误处理逻辑
}

// 全局未捕获的 Promise 错误处理
window.addEventListener('unhandledrejection', (event) => {
  // 阻止默认的错误显示
  event.preventDefault()
  console.error('Unhandled Promise Rejection:', event.reason)
})

// 全局未捕获的错误处理
window.addEventListener('error', (event) => {
  // 阻止默认的错误显示
  event.preventDefault()
  console.error('Global Error:', event.error)
})

app.use(store).use(ElementPlus).use(router).mount('#app')
